package it.unipi.di.rasip.protocol;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;

import javax.microedition.io.Connector;
import javax.microedition.sip.SipClientConnection;
import javax.microedition.sip.SipClientConnectionListener;
import javax.microedition.sip.SipConnectionNotifier;
import javax.microedition.sip.SipDialog;
import javax.microedition.sip.SipException;

public class SipClient extends BusInterface implements SipClientConnectionListener   {
	//private static SipClient _instance;
	private int timeout;
	private SipClientConnection sc;
	private String uri;
	private SipConnectionNotifier scn;
	
	
	public SipClient(String uri,SipConnectionNotifier scn ) {
		this.scn = scn;
		this.uri = uri;
		this.timeout = 5000;
/*		try {
			
			//sc = (SipClientConnection) Connector.open(uri);
			sc.setListener(this);
		} catch (IOException e) {
			System.out.println("SipClient IOException" + uri);
			e.printStackTrace();
		}*/
	}
	
	/** Invia il messaggio msg, utilizzando i dati presenti nel 2' parametro
	 * per creare la send
	 * 
	 * @param msg
	 */
	public void sendMessage( String userUri, String msg ) throws RasipException {
		try {		
			/*if (sc.getDialog() != null) {
				sc = sc.getDialog().getNewClientConnection("MESSAGE");
				System.out.println("dialog" );
			} else {
				
			}*/
			sc = (SipClientConnection) Connector.open(uri);
			sc.setListener(this);
			sc.initRequest("MESSAGE", scn);
			System.out.println("SEND: " + uri + sc.getRequestURI() );
			sc.setHeader("From", userUri ); 
			sc.setHeader("To", uri );		// Destinatario
			sc.setHeader("Subject", "PROVA");	
			sc.setHeader("Content-Type", "text/plain");
			sc.setHeader("Content-Length", Integer.toString(msg.length()));
			OutputStream os = sc.openContentOutputStream();
			os.write(msg.getBytes());
			os.flush();
			os.close(); 	// close stream and send message
		} catch (Exception e) {
			throw new RasipException( e );
		}
	}
	
	public void notifyResponse(SipClientConnection scc) {
		int code = 0;
		try {
			if(scc.receive(timeout)) {  // response received
				code = scc.getStatusCode();
				if(scc.getStatusCode() == 200) {	// handle 200 OK response
//					System.out.println("NF + Risposta ricevuta correttamente");
				} else {					// handle possible error responses
					System.out.println("NF + SCC responce error: " + scc.getStatusCode() );
				}
			} else {
				System.out.println("NF + Risposta NON ricevuta");
			}
			scc.close();
			Mediator.getInstance().alert(uri, code);
			
		} catch (SipException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}



	public void sendRegister() {
		try {
			SipConnectionNotifier scn2;
			//scn2 = (SipConnectionNotifier) Connector.open("sip:5080");
			
			//sc = (SipClientConnection) Connector.open("sip:voip.eutelia.it");
			
		sc.initRequest("REGISTER", scn);
		//sc.setCredentials("1", "2", "3");
		sc.setHeader("From", "sip:paolo@voip.eutelia.it");
		sc.setHeader("To", "sip:paolo@voip.eutelia.it");
		sc.setHeader("Contact", "sip:paolo@87.238.21.79:5060" );
		sc.send();
		
/*		sc.initRequest("REGISTER", null);
		sc.setHeader("From", "sip:paolo@:6000");
		sc.setHeader("Subject", "PROVA");
		sc.setHeader("Content-Type", "text/plain");
		sc.setHeader("Content-Length", Integer.toString(msg.length()));
		
		OutputStream os = sc.openContentOutputStream();
		os.write(msg.getBytes());
		os.close(); // close stream and send message
	*/	
		} catch (SipException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
